﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HRM.DAL.DAO;
using HRM.DAL.Gateway;

namespace HRM.BLL
{
    public class IncrementManager
    {
        IncrementGateway gateway = new IncrementGateway();
        public string SaveIncrement(Increment increment)
        {
            EmployeeManager empManager = new EmployeeManager();
            int empId = empManager.GetEmployeeID(increment.Employee.EmpCode);
            SalaryGateway salaryGateway = new SalaryGateway();
            if (salaryGateway.IsSalaryStructureExistForThisEmployee(empId))
            {
                int incrementNumber = gateway.GetIncrementNumber(empId);
                increment.IncrementNumber = incrementNumber + 1;
                string msg = gateway.SaveIncrement(empId, increment);

                // Go to Updating Salary Sheet
                Code code=new Code();
                Employee employee=new Employee();
                code.CodeName = increment.Employee.EmpCode.CodeName;
                employee.EmpCode = code;
                Salary salary = new Salary();
                salary.Employee=employee;
                salary.Basic = increment.Basic;
                salary.Medical = increment.Medical;
                salary.Residence = increment.Residence;
                salary.Transport = increment.Transport;
                salary.PF = increment.PF;
                salary.Mobile = increment.Mobile;
                salary.Others = increment.Others;
                salary.Gross = increment.Gross;
                SalaryManager salaryManager = new SalaryManager();
                salaryManager.EditEmployeeSalary(salary);

                return msg;
            }
            else
            {
                return "No Salary Structure Exists for this Employee, Please make a Salary Structure first";
            }
        }

        public string EditIncrement(Increment increment)
        {
            EmployeeManager empManager = new EmployeeManager();
            int empId = empManager.GetEmployeeID(increment.Employee.EmpCode);
            
            int incrementNumber = gateway.GetIncrementNumber(empId);
            increment.IncrementNumber = incrementNumber;
            // Go to Edit Last time increment for this Employee
            string msg = gateway.EditIncrement(empId, increment);
            // Go to Updating Salary Sheet
            Salary salary = new Salary();
            salary.Employee.EmpCode = increment.Employee.EmpCode;
            salary.Basic = increment.Basic;
            salary.Medical = increment.Medical;
            salary.Residence = increment.Residence;
            salary.Transport = increment.Transport;
            salary.PF = increment.PF;
            salary.Mobile = increment.Mobile;
            salary.Others = increment.Others;
            salary.Gross = increment.Gross;
            SalaryManager salaryManager = new SalaryManager();
            salaryManager.EditEmployeeSalary(salary);

            return msg;

        }
        public List<Increment> GetIncrementInfo(Increment increment)
        {
            EmployeeManager empManager = new EmployeeManager();
            int empId = empManager.GetEmployeeID(increment.Employee.EmpCode);
            return gateway.GetIncrementInfo(empId);
        } 
    }
}